package com.sugon.controller.module;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleTypeEntity;
import com.sugon.entity.MoldTypePositionEntity;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.service.ModuleTypePositionService;
import com.sugon.service.ModuleTypeService;
import com.sugon.service.SysConfigService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 模具位置关联Controller
 *
 * @author YuXD
 * @date 2023-02-23
 */
@Controller
@RequestMapping("moldTypePosition")
public class MoldTypePositionController {
    @Autowired
    private ModuleTypePositionService moldTypePositionService;
    @Autowired
    private SysConfigService configService;
    @Resource
    private ModuleTypeService moldTypeService;
    @Resource
    @Qualifier("moldTypePositionExcelHandler")
    private IExcelHandler excelHandler;

    /**
     * 查看备件单位列表
     */
    @RequestMapping("/listUnit")
    @ResponseBody
    public R getSparePartUnitList() {
        String sparePartUnit = configService.getValue("MOLD_POSITION_TYPE", "");
        return R.ok().put("moldPositionTypes", sparePartUnit.split(","));
    }

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("modulpositioncorrelation:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<MoldTypePositionEntity> modulPositionCorrelationList = moldTypePositionService.queryList(query);
        int total = moldTypePositionService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(modulPositionCorrelationList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("modulpositioncorrelation:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        MoldTypePositionEntity modulPositionCorrelation = moldTypePositionService.queryObject(id);

        return R.ok().put("modulPositionCorrelation", modulPositionCorrelation);
    }

    /**
     * 保存
     */
    @AutoLog("模具位置关联--保存")
    @RequestMapping("/save")
    @RequiresPermissions("modulpositioncorrelation:save")
    @ResponseBody
    public R save(@RequestBody MoldTypePositionEntity modulPositionCorrelation) {
        moldTypePositionService.save(modulPositionCorrelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("模具位置关联--修改")
    @RequestMapping("/update")
    @RequiresPermissions("modulpositioncorrelation:update")
    @ResponseBody
    public R update(@RequestBody MoldTypePositionEntity modulPositionCorrelation) {
        moldTypePositionService.update(modulPositionCorrelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具位置关联--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("modulpositioncorrelation:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moldTypePositionService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("modulpositioncorrelation:list")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MoldTypePositionEntity> list = moldTypePositionService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/export")
    @ResponseBody
    @AutoLog("执行导出数据")
    public void export(@RequestParam Map<String, Object> queryParam, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        String moldTypeId = MapUtil.getStr(queryParam, "moldTypeId");
        Assert.isTrue(StrUtil.isNotEmpty(moldTypeId), "模具类型必选！");
        ModuleTypeEntity moldType = moldTypeService.queryObject(moldTypeId);
        Assert.notNull(moldType);
        // 设置导出EXCEL模板名称
        modelMap.put(TemplateExcelConstants.FILE_NAME, moldType.getTypeName() + "关联位置导出_" + DateUtil.format(DateUtil.date(), "yyyyMMdd"));
        // 设置导出模板参数
        TemplateExportParams templateExportParams = new TemplateExportParams(configService.getPoiTemplatePath() + "moldTypePositionCorrelationTemplate.xlsx");
        templateExportParams.setColForEach(true);
        modelMap.put(TemplateExcelConstants.PARAMS, templateExportParams);
        // 获取设备维护计划预览模板数据
        Map<String, Object> mapData = cn.hutool.core.map.MapUtil.newHashMap(1);
        // 获取预防性维护计划数据
        mapData.put("list", moldTypePositionService.queryList(queryParam));
        modelMap.put(TemplateExcelConstants.MAP_DATA, mapData);
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }

    /**
     * 导入或更新模具关联位置信息
     */
    @RequestMapping("/import")
    @ResponseBody
    public R importE(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        int count = excelHandler.importExcelFromStream(file.getInputStream());
        return R.ok("成功导入或更新" + count + "条数据");
    }

}
